package com.abso.mp3tunes.locker.ui.views;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;

import com.abso.mp3tunes.locker.ui.ILockerConstants;
import com.abso.mp3tunes.locker.ui.LockerImages;
import com.abso.mp3tunes.locker.ui.LockerPlugin;
import com.abso.mp3tunes.locker.ui.viewers.LockerViewer;
import com.abso.mp3tunes.locker.ui.viewers.SearchViewer;

/**
 * A specialized panel allowing the user to perform a search on the Locker.
 */
public class SearchViewPanel extends LockerBrowserViewPanel {

	/** The search viewer. */
	private SearchViewer viewer;

	/** The text field where the user inserts the text to be searched. */
	private Text textField;
	
	/** The text field where the user inserts the maximum number of results. */
	private Text maxResultsField;

	/** The search button. */
	private Button searchButton;

	/** The artist checkbox. */
	private Button artistCheckbox;

	/** The album checkbox. */
	private Button albumCheckbox;

	/** The track checkbox. */
	private Button trackCheckbox;

	/**
	 * Constructs a new panel.
	 * 
	 * @param parent
	 *            the parent control.
	 * @param lockerBrowserView
	 *            the locker browser view.
	 */
	public SearchViewPanel(Composite parent, LockerBrowserView lockerBrowserView) {
		super(parent, lockerBrowserView);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.abso.mp3tunes.locker.ui.views.LockerBrowserViewPanel#createControl(org.eclipse.swt.widgets.Composite)
	 */
	public Composite createControl(Composite parent) {
		Composite mainComp = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout(1, true);
		layout.marginWidth = 0;
		layout.marginHeight = 0;
		mainComp.setLayout(layout);

		/* adds the search controls */
		Composite textComp = new Composite(mainComp, SWT.NONE);
		layout = new GridLayout(2, false);
		layout.marginWidth = 5;
		layout.marginHeight = 0;
		textComp.setLayout(layout);
		textComp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		textField = new Text(textComp, SWT.SINGLE | SWT.LEFT | SWT.BORDER);
		GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
		textField.setLayoutData(gd);
		searchButton = new Button(textComp, SWT.PUSH);
		searchButton.setText("Search");
		searchButton.setToolTipText("Search");
		searchButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false,
				false));
		searchButton.setEnabled(false);
		Composite typeComp = new Composite(mainComp, SWT.NONE);
		layout = new GridLayout(5, false);
		layout.marginWidth = 5;
		layout.marginHeight = 0;
		typeComp.setLayout(layout);
		typeComp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		artistCheckbox = new Button(typeComp, SWT.CHECK);
		artistCheckbox.setToolTipText("Artists");
		artistCheckbox.setImage(LockerImages.get(LockerImages.ARTIST));
		albumCheckbox = new Button(typeComp, SWT.CHECK);
		albumCheckbox.setToolTipText("Albums");
		albumCheckbox.setImage(LockerImages.get(LockerImages.ALBUM));
		trackCheckbox = new Button(typeComp, SWT.CHECK);
		trackCheckbox.setImage(LockerImages.get(LockerImages.TRACK));
		trackCheckbox.setToolTipText("Tracks");
		trackCheckbox.setSelection(true);
		new CLabel(typeComp, SWT.NONE).setText("Max Results:");
		maxResultsField = new Text(typeComp, SWT.SINGLE | SWT.LEFT | SWT.BORDER);
		maxResultsField.setText(Integer.toString(LockerPlugin.getDefault().getPreferenceStore().getInt(ILockerConstants.PREF_MAX_SEARCH_RESULTS)));
		gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
		maxResultsField.setLayoutData(gd);

		/* adds listeners */
		textField.addModifyListener(new ModifyListener() {

			/*
			 * (non-Javadoc)
			 * 
			 * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
			 */
			public void modifyText(ModifyEvent e) {
				updateSearchButtonState();
			}
		});
		textField.addKeyListener(new KeyListener() {
			public void keyPressed(KeyEvent e) {
			}

			public void keyReleased(KeyEvent e) {
				if (e.character == SWT.CR) {
					if (searchButton.isEnabled()) {
						viewer.updateContent();
					}
				}
			}
		});
		SelectionListener selectionListener = new SelectionListener() {

			/*
			 * (non-Javadoc)
			 * 
			 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
			 */
			public void widgetDefaultSelected(SelectionEvent e) {
			}

			/*
			 * (non-Javadoc)
			 * 
			 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
			 */
			public void widgetSelected(SelectionEvent e) {
				updateSearchButtonState();
			}
		};
		artistCheckbox.addSelectionListener(selectionListener);
		albumCheckbox.addSelectionListener(selectionListener);
		trackCheckbox.addSelectionListener(selectionListener);
		searchButton.addSelectionListener(new SelectionListener() {
			/*
			 * (non-Javadoc)
			 * 
			 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
			 */
			public void widgetDefaultSelected(SelectionEvent e) {
			}

			/*
			 * (non-Javadoc)
			 * 
			 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
			 */
			public void widgetSelected(SelectionEvent e) {
				
				/* saves the max results as a preference */
				int maxResults = NumberUtils.toInt(maxResultsField.getText(), 0);
				maxResultsField.setText(Integer.toString(maxResults));
				LockerPlugin.getDefault().getPreferenceStore().setValue(ILockerConstants.PREF_MAX_SEARCH_RESULTS, maxResults);
				
				/* performs the search */
				viewer.updateContent();
			}
		});

		/* adds the tree viewer */
		viewer = new SearchViewer(mainComp, getLockerBrowserView(), textField,
				maxResultsField, artistCheckbox, albumCheckbox, trackCheckbox);
		viewer.getTree().setLayoutData(
				new GridData(SWT.FILL, SWT.FILL, true, true));
		return mainComp;

	}

	/** Updates the enabled state of the search button. */
	protected void updateSearchButtonState() {
		if (StringUtils.isEmpty(textField.getText())) {
			searchButton.setEnabled(false);
		} else {
			searchButton.setEnabled(artistCheckbox.getSelection()
					|| albumCheckbox.getSelection()
					|| trackCheckbox.getSelection());
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.abso.mp3tunes.locker.ui.views.LockerBrowserViewPanel#getLockerViewer()
	 */
	protected LockerViewer getLockerViewer() {
		return viewer;
	}

}
